package almacen;

import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import resources.local_connection;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Tesla
 */
public final class Escasos extends javax.swing.JDialog {
Connection cn;
    PreparedStatement ps;
    ResultSet rs;
    DefaultTableModel dtm;
    /**
     * Creates new form Escasos
     */
    public Escasos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        dtm = (DefaultTableModel) this.jTable1.getModel();
        TableRowSorter<TableModel> elQueOrdena = new TableRowSorter<TableModel>(dtm);
jTable1.setRowSorter(elQueOrdena);
        int[] anchos = {40, 20, 40, 40};
        for (int i = 0; i < jTable1.getColumnCount(); i++) {
            jTable1.getColumnModel().getColumn(i).setPreferredWidth(anchos[i]);
        }
        dtm.setRowCount(0);
        try {
            cn = local_connection.getconnect();
            

        } catch (Exception e) {
            Almacen.logger.log(Level.SEVERE, null, e);
        }
        cargarDatos();
    }
void cargarDatos(){
    try {
        ps = (PreparedStatement) cn.prepareStatement("SELECT a.id_producto,a.nombre,b.minimo,b.cantidad,b.cantidad FROM productos a LEFT OUTER JOIN stock b ON (a.id_producto = b.id_producto) WHERE b.cantidad <= b.minimo");
               rs = ps.executeQuery();
               ArrayList<Object[]> data = new ArrayList<>();

               while (rs.next()) {
                   Object[] rows = new Object[jTable1.getColumnCount()];
                   for (int i = 0; i < rows.length-1; i++) {
                       rows[i] = rs.getObject(i + 1);
                   }
                   rows[3]=rs.getInt(4)+rs.getInt(5);
                   data.add(rows);
               }


               for (int i = 0; i < data.size(); i++) {
                   dtm.addRow(data.get(i));
               }
    } catch (SQLException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);    }
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Productos bajos en stock");
        setIconImage(new ImageIcon(getClass().getResource("/resources/logo_rayito.png")).getImage());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Cantidad", "Minimo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(20);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(180);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(20);
        jTable1.getColumnModel().getColumn(3).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(20);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
